package com.mentaleye.controller;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.AssessmentBean;
import com.mentaleye.bean.AutoFormBean;
import com.mentaleye.biz.*;
import com.mentaleye.dal.model.Assessment;
import com.mentaleye.enums.CommonStatus;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 考核列表
 */
@Controller
@RequestMapping({"/assessment"})
public class AssessmentController {
    private static Logger logger = Logger.getLogger(AssessmentController.class);
    @Autowired
    private DictionaryBiz dictionaryBiz;
    @Autowired private OaClassBiz oaClassBiz;
    @Autowired
    private AutoFormBiz autoFormBiz;
    @Autowired
    TeacherBiz teacherBiz;
    @RequestMapping(value = "/list/{assessmentType}")
    public ModelAndView list(@PathVariable("assessmentType") Integer assessmentType) {
        ModelAndView view = new ModelAndView("cm/assessment/assessmentList");
        view.addObject("assessmentType",  assessmentType);
        Map<Integer,String> assessmentTypeMap=dictionaryBiz.queryDictionaryByParent(DictionaryEnums.FORM_TYPE.getCode());
        //view.addObject("assessmentTypeMap", assessmentTypeMap);
        view.addObject("assessmentTypeName", assessmentTypeMap.get(assessmentType));
        view.addObject("statusMap",JSONObject.toJSONString(CommonStatus.enumMap) );
        return view;
    }

    @RequestMapping("/listData")
    @ResponseBody
    public String listData( CommonPage commonPage) {
        CommonBizImpl.queryByPage("Assessment",commonPage);
        return commonPage.toSHowPage();
    }

    @RequestMapping({"/edit"})
    public ModelAndView edit(@RequestParam(value="id", required=false) Integer id,@RequestParam(value="assessmentType") Integer assessmentType) {
        ModelAndView view = new ModelAndView("cm/assessment/assessmentEdit");
        view.addObject("assessmentType",  assessmentType);

        view.addObject("oaClassList", oaClassBiz.getAllOaClass());
        //需要选择教师 时候使用
        view.addObject("teachList",teacherBiz.queryAllTeacher());

        Map<String,Object> map=new HashMap<String,Object>();
        map.put("type",assessmentType);
        view.addObject("formList",autoFormBiz.queryAutoFormByMap(map));

        if (id != null) {
            Assessment assessment=(Assessment)CommonBizImpl.getBeanById("Assessment","id",id,false);
            view.addObject("assessment",assessment);
        }
        return view;
    }

    @RequestMapping(value = "/save")
    public @ResponseBody String save( AssessmentBean assessmentBean) {
        return JSONObject.toJSONString(CommonBizImpl.edit(assessmentBean,"Assessment","id","assessmentStatus"));
    }

    @RequestMapping(value = "/delete")
    public @ResponseBody String delete(@RequestBody List<Integer> ids) {
        return JSONObject.toJSONString(CommonBizImpl.delete("Assessment",ids,"id","assessmentStatus"));
    }

    @RequestMapping(value = "/getAssessmentFieldSelect")
    public  @ResponseBody
    CommonResult getAssessmentFieldSelect(@RequestParam(value="autoFormVersion" )Integer autoFormVersion ,@RequestParam(value="autoFormId" )Integer autoFormId ,@RequestParam(value="selectField", required=false)String selectField){

        CommonResult commonResult=new CommonResult();
        StringBuffer stringBuffer=new StringBuffer("<option value='' >请选择总分字段</option>");
        try{
            double  score=0d;
            AutoFormBean formBean=autoFormBiz.getFormById(autoFormId,autoFormVersion);
                if(formBean!=null){
                    String parse=formBean.getParse();
                    String columnStr=parse.substring(parse.indexOf("\"columns\":[")+11);
                    columnStr=columnStr.substring(0,columnStr.indexOf("]")).replaceAll("\"","");
                    String[] columns= columnStr.split(",");

//                    Double scoreSingle;
//                    String  scoreStr;
                    if(columns!=null){
                        for (String column:columns){
//                            //截取分数 计算每一个项目的分数
//                            if(column.indexOf("(")>0){
//                                scoreStr=column.substring(column.indexOf("(")+1,column.indexOf(")"));
//                                try {
//                                    scoreSingle = Double.parseDouble(scoreStr);
//                                    score=score+scoreSingle;
//                                }catch (Exception e){
//                                }
//                            }
                            stringBuffer.append("<option value='").append(column).append("'");
                            if(StringUtils.isNotEmpty(selectField)&&selectField.equals(column)){
                                stringBuffer.append("selected='selected'");
                            }
                            stringBuffer.append(">").append(column).append("</option>");
                        }
                    }
                }
            Map<String,Object> data=new HashMap<String, Object>();
            data.put("html",stringBuffer.toString());
           // data.put("score",score);
            commonResult.setData(data);
            commonResult.setIsSuccess(true);
        }catch (Exception e){
            logger.error("获取字段异常",e);
        }
        return commonResult;
    }


    @RequestMapping({"/changeStatus"})
    public  @ResponseBody
    CommonResult changeStatus(@RequestParam(value="id", required=false) Integer id,@RequestParam(value="status") Integer status) {
        AssessmentBean assessmentBean=new AssessmentBean();
        assessmentBean.setId(id);
        assessmentBean.setAssessmentStatus(status);
       return CommonBizImpl.edit(assessmentBean,"Assessment","id","assessmentStatus");

    }





}
